#!/bin/sh
#
# shell script to grant privileges to the bacula database
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
db_user=${db_user:-@db_user@}
bindir=@MYSQL_BINDIR@
db_name=${db_name:-@db_name@}
db_password=@db_password@
if [ "$db_password" != "" ]; then
   pass="identified by '$db_password'"
fi
db_ssl_options=@db_ssl_options@
if [ "$db_ssl_options" != "" ]; then
   ssl_options="require $db_ssl_options"
fi
 
if $bindir/mysql $* -u root -f 2>/dev/null 1>/dev/null  <<EOD
use mysql;
create user ${db_user} ${pass};
create user ${db_user}@"%" ${pass};
create user ${db_user}@localhost ${pass};
EOD
then
  echo "Created MySQL database user: ${db_user}"
fi

if $bindir/mysql $* -u root -f <<END-OF-DATA
use mysql
grant all privileges on ${db_name}.* to ${db_user}@"%" ${ssl_options};
grant process on *.* to ${db_user}@"%" ${ssl_options};
select * from user;
flush privileges;
END-OF-DATA
then
   echo "Privileges for user ${db_user} granted on database ${db_name}."
   exit 0
else
   echo "Error creating privileges."
   exit 1
fi
